package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.tim.app.R;
import com.tim.app.constant.EventTag;

import java.util.List;

/**
 * 搜索历史记录
 */
public class HistorySearchAdapter extends BaseRecyclerAdapter<HistorySearchAdapter.HistorySearchViewHolder, String> {

    private Context mContext;
    private List<String> datas;

    public HistorySearchAdapter(Context mContext, List<String> data) {
        super(data);
        this.mContext = mContext;
        this.datas = data;
    }

    @Override
    public HistorySearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_search_history_item, parent, false);
        return new HistorySearchViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HistorySearchViewHolder holder, final int position, final String data) {
        if (TextUtils.isEmpty(data)) {
            return;
        }
        holder.tvHistoryData.setText(data);
        holder.ibDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventManager.ins().sendEvent(EventTag.DO_DELETE_SEARCH_HISTORY, 0, 0, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    static class HistorySearchViewHolder extends BaseRecyclerAdapter.BaseRecyclerViewHolder {
        private TextView tvHistoryData;
        private ImageButton ibDelete;

        public HistorySearchViewHolder(View itemView) {
            super(itemView);
            tvHistoryData = (TextView) itemView.findViewById(R.id.tvHistoryData);
            ibDelete = (ImageButton) itemView.findViewById(R.id.ibDelete);
        }
    }
}
